using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using System;
using System.Collections.Generic;
namespace MicroMsg.Manager
{
	public class VUserInfoMgr
	{
		private const string TAG = "VUserInfoMgr";
		private VUserInfoMap mVUserInfoMap;
		private bool mIsUpdating;
		public byte[] getVUserIcon(uint verifyFlag, bool isHD = false)
		{
			this.checkUpdate();
			if (this.mVUserInfoMap == null || this.mVUserInfoMap.mListVUserInfo.get_Count() <= 0)
			{
				return null;
			}
			VUserInfo itemByVerifyFlag = this.getItemByVerifyFlag(verifyFlag);
			if (itemByVerifyFlag == null)
			{
				return null;
			}
			if (isHD)
			{
				return itemByVerifyFlag.mHDIcon;
			}
			return itemByVerifyFlag.mIcon;
		}
		private VUserInfo getItemByID(uint uint_0)
		{
			VUserInfo result;
			using (List<VUserInfo>.Enumerator enumerator = this.mVUserInfoMap.mListVUserInfo.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					VUserInfo current = enumerator.get_Current();
					if (current.mID == uint_0)
					{
						result = current;
						return result;
					}
				}
				goto IL_46;
			}
			return result;
			IL_46:
			return null;
		}
		private VUserInfo getItemByVerifyFlag(uint verifyFlag)
		{
			for (uint num = 2147483648u; num != 0u; num >>= 1)
			{
				if ((verifyFlag & num) != 0u)
				{
					VUserInfo itemByID = this.getItemByID(num);
					if (itemByID != null)
					{
						return itemByID;
					}
				}
			}
			return null;
		}
		public bool checkUpdate()
		{
			if (!AccountMgr.hasValidAccount())
			{
				return false;
			}
			if (this.mVUserInfoMap == null)
			{
				this.mVUserInfoMap = GConfigMgr.read<VUserInfoMap>();
			}
			if (this.mVUserInfoMap == null)
			{
				this.mVUserInfoMap = new VUserInfoMap();
			}
			if (Util.getNowSeconds() < this.mVUserInfoMap.mLastUpdatedTimeStamp + 86400.0)
			{
				return true;
			}
			if (this.mIsUpdating)
			{
				return false;
			}
			this.doUpdateVUserInfo();
			return false;
		}
		private void doUpdateVUserInfo()
		{
			this.mIsUpdating = true;
			NetSceneGetVUserInfo netSceneGetVUserInfo = new NetSceneGetVUserInfo();
			netSceneGetVUserInfo.mOnSceneFinished += new onSceneFinishedDelegate(this.onUpdateVUserInfo);
			netSceneGetVUserInfo.doScene(this.mVUserInfoMap.mVersion);
		}
		private void onUpdateVUserInfo(bool bool_0, GetVUserInfoResponse resp)
		{
			this.mIsUpdating = false;
			if (bool_0 && resp != null && resp.VUserListCount > 0)
			{
				this.mVUserInfoMap = new VUserInfoMap();
				this.mVUserInfoMap.mUserName = AccountMgr.getCurAccount().strUsrName;
				this.mVUserInfoMap.mVersion = resp.Version;
				this.mVUserInfoMap.mLastUpdatedTimeStamp = Util.getNowSeconds();
				using (IEnumerator<VUserResponseItem> enumerator = resp.VUserListList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						VUserResponseItem current = enumerator.get_Current();
						VUserInfo vUserInfo = new VUserInfo();
						vUserInfo.mID = current.ID;
						vUserInfo.mDesc = current.Desc;
						vUserInfo.mIcon = current.Icon.Buffer.ToByteArray();
						vUserInfo.mHDIcon = current.HDIcon.Buffer.ToByteArray();
						this.mVUserInfoMap.mListVUserInfo.Add(vUserInfo);
					}
				}
				GConfigMgr.write<VUserInfoMap>(this.mVUserInfoMap);
				EventCenter.postEvent(EventConst.ON_NETSCENE_VUSERINFO_UPDATED, null, null);
				return;
			}
		}
	}
}
